package b2.widget.sendMail
{
	import b2.config.*;
	import b2.net.ResourceHelper;
	import b2.player.PlayerInfo;
	import b2.player.PlayerInfoManager;
	import b2.player.dataModel.Mail;
	import b2.player.dataModel.ObjWithNum;
	import b2.player.event.MailEvent;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tech.ObjectSlot;
	import b2.widget.warehouse.*;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	public class SendMailTab extends Sprite
	{
		private static var instance:SendMailTab;
		
		public var objectSlots:Array;
		
		public var mail:Mail;
		
		public var idText:TextField;
		
		private var message:TextField;
		
		public function SendMailTab(mail:Mail)
		{
			this.mail = mail;
			var TankBgr:Class = ResourceHelper.getInstance().getClassByName("TankDialogBgr");
			var backgroundImage:Sprite = new TankBgr();
			addChild(backgroundImage);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 16, 0xFFFFFF);
        	titleFomat.letterSpacing = 10;
			titleText.defaultTextFormat = titleFomat;
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = "邮件";
			titleText.x = (this.width - titleText.width) / 2;
			titleText.y = 13;
			addChild(titleText);
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton");
			var closeButton:BasicButton = new BasicButton(CloseButton);
			closeButton.x = this.width - 42;
			closeButton.y = 12;
			addChild(closeButton);
			closeButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
			
			var contentFormat:TextFormat = new TextFormat("微软雅黑", 14, 0xFFFFFF);
			//id 说明
			var idTextStatic:TextField = new TextField();
			idTextStatic.selectable = false;
			idTextStatic.x = 36;
			idTextStatic.y = 70;
			idTextStatic.defaultTextFormat = new TextFormat("微软雅黑", 14, 0x000000);
			idTextStatic.text = "收件人ID:";
			addChild(idTextStatic);
			
			//背景
			var rrect:Sprite = new Sprite();
			rrect.graphics.beginFill(0x27AAE1);
			rrect.graphics.drawRoundRect(0, 0, 195, 22, 15, 15);
			rrect.graphics.endFill();
			rrect.alpha = 0.8;
			rrect.x = 110;
			rrect.y = idTextStatic.y;
			addChild(rrect);
			//toId 输入框
			idText = new TextField();
//			idText.background = true;
//			idText.backgroundColor = 0x27AAE1;
			idText.width = 190;
			idText.height = 22;
			idText.x = 115;
			idText.y = idTextStatic.y;
			idText.defaultTextFormat = contentFormat;
			idText.text = "";
			if (PlayerInfoManager.getInstance().me.isAdmin || Profile.DEBUG != 0)
				idText.type = TextFieldType.INPUT;
			else
				idText.selectable = false;
			addChild(idText);
			idText.addEventListener(Event.CHANGE, onIdTextChange);
			
			//内容
			var messageStatic:TextField = new TextField();
			messageStatic.selectable = false;
			messageStatic.x = 36;
			messageStatic.y = 95;
			messageStatic.defaultTextFormat = new TextFormat("微软雅黑", 14, 0x000000);
			messageStatic.text = "留言：";
			addChild(messageStatic);
			
			//背景
			rrect = new Sprite();
			rrect.graphics.beginFill(0x27AAE1);
			rrect.graphics.drawRoundRect(0, 0, 270, 120, 15, 15);
			rrect.graphics.endFill();
			rrect.alpha = 0.8;
			rrect.x = 35;
			rrect.y = 120;
			addChild(rrect);
			//消息输入框
			message = new TextField();
//			message.background = true;
//			message.backgroundColor = 0x27AAE1;
			message.width = 260;
			message.height = 120;
			message.maxChars = 40;
			message.text = "";
			message.type = TextFieldType.INPUT;
			message.defaultTextFormat = contentFormat;
			message.x = 40;
			message.y = 120;
			message.multiline = true;
			message.wordWrap = true;
			addChild(message);
			message.addEventListener(Event.CHANGE, onMailTextChange);
			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			var sendButton:BasicButton = new BasicButton(ButtonBgr, "发送邮件");
			sendButton.x = 220;
			sendButton.y = 290;
			addChild(sendButton);
			sendButton.addEventListener(MouseEvent.CLICK, onSendButtonButtonClick);
			//附件
			var attachmentText:TextField = new TextField();
			attachmentText.selectable = false;
			attachmentText.x = 36;
			attachmentText.y = 250;
			attachmentText.defaultTextFormat = new TextFormat("微软雅黑", 14, 0x000000);
			attachmentText.text = "附件：";
			addChild(attachmentText);
			
			objectSlots = new Array();
			var objectSlot:ObjectSlot = new ObjectSlot();
			objectSlot.x = 30;
			objectSlot.y = 280;
			addChild(objectSlot);
			objectSlots.push(objectSlot);
			objectSlot = new ObjectSlot();
			objectSlot.x = 85;
			objectSlot.y = 280;
			addChild(objectSlot);
			objectSlots.push(objectSlot);
			objectSlot = new ObjectSlot();
			objectSlot.x = 140;
			objectSlot.y = 280;
			addChild(objectSlot);
			objectSlots.push(objectSlot);
			mail.addEventListener(MailEvent.MAIL_UPDATE, mailUpdate);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		private function onAddedToStage(event:Event):void
		{
//			removeAll();
		}
		
		private function onRemovedFromStage(event:Event):void
		{
			removeAll();
		}
		
		private function removeAll():void
		{
			mail.setMessage("");
			mail.addObjectPackageItems();
			mail.setGiftArray(null);
		}
		
		public function onIdTextChange(event:Event):void
		{
			mail.setToId(parseInt(idText.text));
		}
		
		public function onMailTextChange(event:Event):void
		{
			mail.setMessage(message.text);
		}
		
		public function mailUpdate(event:MailEvent):void
		{
			idText.removeEventListener(Event.CHANGE, onIdTextChange);
			message.removeEventListener(Event.CHANGE, onMailTextChange);
			idText.text = mail.toId.toString();
			message.text = mail.message;
			
			for(var i:int = 0; i < objectSlots.length; i++)
			{
				var objSlot:ObjectSlot = objectSlots[i];
				objSlot.removeObj();
				
				if (mail.giftArray.length > i)
				{
					var gift:ObjWithNum = mail.giftArray[i];
					objSlot.loadObj(gift.obj.objectIndex, gift.obj.basicLevel, gift.obj.strengthenLevel, 1, gift.num, 0, false, false, true);
				}
			}
			idText.addEventListener(Event.CHANGE, onIdTextChange);
			message.addEventListener(Event.CHANGE, onMailTextChange);
		}
		
		public function onCloseButtonClick(event:MouseEvent):void
		{
			close();
			Warehouse.getInstance().close();
		}
		
		public function onSendButtonButtonClick(event:MouseEvent):void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			if (me.getObjNum(CommonObjConfig.OIL, 0, 0) < Config.basicConfig.postage)
			{
				var popMessage:PopMessage = new PopMessage("石油不足，需要 " + Config.basicConfig.postage + " 石油！", 0xFF0000);
				WidgetManager.getInstance().popOut([popMessage], false, true);
			}
			else
			{
				var messageBox:MessageBox = new MessageBox("需要消耗<font color='#00AA00'> " + Config.basicConfig.postage + 
					" </font>石油，是否继续？", true);
				messageBox.buttonOK.addEventListener(MouseEvent.CLICK, onSendMail);
				WidgetManager.getInstance().popOut([messageBox], false, true);
			}
		}
		
		private function onSendMail(event:MouseEvent):void
		{
			mail.send();
		}
		
		public static function getInstance():SendMailTab
		{
			if (!instance) 
				instance = new SendMailTab(new Mail(PlayerInfoManager.getInstance().myUserId));
			return instance;
		}
		
		public function close():void
		{
			if (this.parent != null)
			{
				this.parent.removeChild(this);
			}
		}
	}
}